MatchesPreseason.tsx ➔ getDataA   C
last analyzed

Complexity

Conditions 9

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 6.6666
c 0
b 0
f 0
cc 9
1
import axios from "axios"
2
import classNames from "classnames"
3
import { graphql, useStaticQuery } from "gatsby"
4
import moment from "moment-timezone"
5
import "moment-timezone/node_modules/moment/locale/nl-be"
6
import React, { FunctionComponent, useEffect, useState } from "react"
7
import LazyLoad from "react-lazy-load"
8
9
import { capitalizeFirstLetter, groupByMonth, mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
10
import "./MatchesPreseason.scss"
11
import Spinner from "./Spinner"
12
13
const MatchOverviewMatch: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
14
  moment.tz.setDefault(`Europe/Brussels`)
15
  moment.locale(`nl-be`)
16
  moment.localeData(`nl-be`)
17
18
  const d = moment(match.date)
19
  const matchPlayed =
20
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
21
    false
22
23
  return (
24
    <article>
25
      <main className="matches__calendar__main matches__calendar__preseason">
26
        <div className="matches__calendar__date matches__preseason__date">
27
          <span className="matches__calendar__date matches__preseason__date--date">{d.format(`DD`)}</span>
28
          <span className="matches__calendar__date matches__preseason__date--day">
29
            {capitalizeFirstLetter(d.format(`dddd`))}
30
          </span>
31
          <span className="matches__preseason__divider"> // </span>
32
          <span className="matches__calendar__date matches__preseason__date--day">{d.format(`HH:mm`)}</span>
33
        </div>
34
35
        <div className="matches__preseason__match">
36
          <div
37
            className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
38
              "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
39
            })}
40
          >
41
            {match.homeClub?.name} {match.homeTeamId === null || (match.homeTeamId === 1 ? `A` : `B`)}
42
            <LazyLoad>
43
              <img
44
                src={match.homeClub?.logo}
45
                alt={match.homeClub?.name}
46
                className="matches__calendar__logo matches__calendar__logo--home"
47
              />
48
            </LazyLoad>
49
          </div>
50
51
          <div className="matches__calendar__score">
52
            {match.status !== 0 && (
53
              <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
54
            )}
55
            {(match.status === 0 || match.status === null) && !matchPlayed && <span>-</span>}
56
            {matchPlayed && (
57
              <span>
58
                {match.goalsHomeTeam} - {match.goalsAwayTeam}
59
              </span>
60
            )}
61
          </div>
62
63
          <div
64
            className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
65
              "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
66
            })}
67
          >
68
            <LazyLoad>
69
              <img
70
                src={match.awayClub?.logo}
71
                alt={match.awayClub?.name}
72
                className="matches__calendar__logo matches__calendar__logo--away"
73
              />
74
            </LazyLoad>
75
            {match.awayClub?.name} {match.awayTeamId === null || (match.awayTeamId === 1 ? `A` : `B`)}
76
          </div>
77
        </div>
78
        <div className="matches__calendar__type matches__preseason__type">{match.competitionType}</div>
79
      </main>
80
    </article>
81
  )
82
}
83
84
const MatchesOverview: FunctionComponent<MatchesProps> = () => {
85
  const [dataA, setDataA] = useState<Match[]>([])
86
  const [dataB, setDataB] = useState<Match[]>([])
87
88
  const {
89
    site: {
90
      siteMetadata: { kcvvPsdApi },
91
    },
92
  }: MatchesQueryData = useStaticQuery(graphql`
93
    {
94
      site {
95
        siteMetadata {
96
          kcvvPsdApi
97
        }
98
      }
99
    }
100
  `)
101
102
  useEffect(() => {
103
    async function getDataA() {
104
      const response = await axios.get(`${kcvvPsdApi}/matches/1`)
105
      setDataA(response.data)
106
    }
107
    async function getDataB() {
108
      const response = await axios.get(`${kcvvPsdApi}/matches/2`)
109
      setDataB(response.data)
110
    }
111
    getDataA()
112
    getDataB()
113
  }, [])
114
115
  const data = groupByMonth(dataA.concat(dataB))
116
117
  return (
118
    <div className={`preseason__wrapper`}>
119
      {data.length > 0 || <Spinner />}
120
      {data.map(({ date, objects }, j: number) => (
121
        <>
122
          <h3>{date}</h3>
123
          {objects
124
            .sort((a: Match, b: Match) => a.timestamp - b.timestamp)
125
            .map((match: Match, i: number) => (
126
              <MatchOverviewMatch match={match} key={match.id} />
127
            ))}
128
        </>
129
      ))}
130
    </div>
131
  )
132
}
133
134
export default MatchesOverview
135